package com.moyun.common.utils;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.moyun.model.po.bpm.BpmProcess;
import org.junit.Test;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.stream.Collectors;

/**
 * <h3>moyun-base</h3>
 * <p>数据库工具类</p>
 *
 * @author : ybx
 * @date : 2021-10-14 11:50:05
 **/
public class DBUtil {

    /**
     * @return java.lang.String
     * @Author yangboxuan
     * @Description 将实体类属性转换为数据库对应字段的字符串
     * @Date 2021/10/14 15:02
     * @Param [clazz, ColumnAlias, prefix]
     **/
    public static String entity2ColumnStr(Class<?> clazz, String ColumnAlias, String prefix) {
        ArrayList<String> entityFieldStrs = new ArrayList<>();
        BeanUtil.descForEach(clazz, (propDesc -> entityFieldStrs.add(propDesc.getFieldName())));

        String entityColumnStr;
        if (StringUtils.isEmpty(prefix)) {
            entityColumnStr = cn.hutool.core.util.StrUtil.join(",", entityFieldStrs.stream().map(cn.hutool.core.util.StrUtil::toUnderlineCase).collect(Collectors.toList()));
        } else {
            entityColumnStr = cn.hutool.core.util.StrUtil.join(",", entityFieldStrs.stream().map(entityfieldStr -> ColumnAlias + "." + StrUtil.toUnderlineCase(entityfieldStr) + " as " + prefix + StrUtil.toUnderlineCase(entityfieldStr)).collect(Collectors.toList()));

        }
        return entityColumnStr;

    }

    /**
     * @Author yangboxuan
     * @Description 测试用例
     * @Date 2022/9/3 16:08
     * @Param []
     */
    @Test
    public void testCase() {
        String s = DBUtil.entity2ColumnStr(BpmProcess.class, "bp", "bp_");
        System.out.println(s);
    }
}
